<template>
  <div>
    <el-card>
      <template slot="header" style="display: inline-block">
        <div class="title">实验室成员</div>
        <div class="subtitle">labor member</div>
      </template>
      <el-table
        :data="userList"
        style="width: 100%">
        <el-table-column
          type="index"
          label="序号"
          width="100">
        </el-table-column>
        <el-table-column
          label="姓名"
          width="500">
          <template slot-scope="scope">
            <el-popover trigger="hover" placement="top">
              <p>姓名: {{ scope.row.username }}</p>
              <div slot="reference" class="name-wrapper">
                <el-tag size="medium">{{ scope.row.username }}</el-tag>
              </div>
            </el-popover>
          </template>
        </el-table-column>
        <el-table-column label="操作">
          <template slot-scope="scope">
            <el-button
              size="mini"
              @click="handleEdit(scope.row)" v-if="($store.state.authority===4&&scope.row.authority<4)||$store.state.id===scope.row.id">编辑
            </el-button>
<!--            <el-button-->
<!--              size="mini"-->
<!--              type="danger"-->
<!--              @click="handleDelete(scope.row)" v-if="$store.state.authority===4||$store.state.id===scope.row.id">注销-->
<!--            </el-button>-->
          </template>
        </el-table-column>
      </el-table>
    </el-card>
    <el-row>
      <el-pagination
        @current-change="handleCurrentChange"
        :current-page="currentPage"
        :page-size="pageSize"
        :total="userList.length"
        class="pagination">
      </el-pagination>
    </el-row>
    <edit-user ref="edit" @clear="getUserList"></edit-user>
  </div>
</template>

<script>
import EditUser from "../admin/content/labor/EditUser";

export default {
  name: "UserList",
  components:{EditUser},
  data() {
    return {
      userList: [],
      //页号
      currentPage: 1,
      pageSize: 15,
    }
  },
  methods: {
    /*
    加载
     */
    getUserList() {
      this.$axios.get(`/user/userList/page=${this.currentPage - 1}`).then(res => {
        this.userList = res.data.data.filter(item=>{
          return item.authority!==0
        })
        this.userList.forEach((item,index,array)=>{
          if(item.id===this.$store.state.id){
            array.splice(index,1)
            array.unshift(item)
          }
        })
      }).catch(failRes => {
        this.userList=[]
        console.log(failRes)
      })
    },
    /*
      编辑
     */
    handleEdit(user) {
      if(this.$store.state.authority<4){
        this.$router.push({
          path:'/space'
        })
      }
      else{
        this.$axios.get(`/user/info/id=${user.id}`).then(res=>{
          this.$refs.edit.dialogFormVisible=true
          this.$refs.edit.form={
            id:user.id,
            username: res.data.data.username,
            password: res.data.data.password,
            phone:res.data.data.phone,
            email: res.data.data.email,
            authority:user.authority
          }
        }).catch(failRes=>console.log(failRes))
      }
    },

    /*
      处理页号
     */
    handleCurrentChange(page) {
      this.currentPage = page
      this.getUserList()
    },
  },
  mounted() {
    this.getUserList()
  }
}
</script>

<style scoped>
.title{
  font-size: 30px;
  font-weight: 700;
}
.subtitle{
  font-size: 20px;
  color:#888888;
  margin-left: 15px;
  margin-bottom: -15px;
}
</style>
